package com.androidian.tippit.fragments;

import com.androidian.tippit.ActivityHome;
import com.androidian.tippit.R;
import com.androidian.tippit.utilities.Callbacks.StandardVariables;
import com.androidian.tippit.utilities.Callbacks.WizardLocker;
import com.androidian.tippit.utilities.Fonts;

import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class FragmentBillTotal extends Fragment 
{
	
	private WizardLocker mWizardLocker;
	private StandardVariables mStandardVariables;
	
	private EditText mEditText;
	
	private double billTotal;
	
	private ActivityHome mActivity;


    
	@Override
	public void onAttach(Activity activity) 
	{
		super.onAttach(activity);
		
		mWizardLocker = (WizardLocker) activity;

		mStandardVariables = (StandardVariables) activity;
		
		mActivity = (ActivityHome) activity;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		
		final View view = inflater.inflate(R.layout.fragment_billtotal,
				container, false);		
		
		mWizardLocker.lockNavigation(true, true, true);
		
		Fonts.setRobotoThinFont(this.getActivity(), view);
		
		mEditText = (EditText)
				view.findViewById(R.id.billEditText);
		
		mEditText.addTextChangedListener(mTextWatcher);
		
			if(mActivity.billTotal > 0)
			{
				
				mEditText.setText(String.valueOf(billTotal));
				
			}
			
		return view;
		
	}
	
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) 
	{
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) 
		{
			
			mWizardLocker.lockNavigation(true, false, false);
							
		}

	}

	
	private TextWatcher mTextWatcher = new TextWatcher()
	{
		
		@Override
		public void afterTextChanged(Editable s)
		{			

			// Not used
			
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) 
		{

			// Not used
			
		}

		@Override
		public void onTextChanged(CharSequence text, int start, int before, int count) 
		{				
						
				try
				{
					
					billTotal = Double.parseDouble(text.toString());
					
				}
				
				catch(NumberFormatException exception)
				{
					
					billTotal = 0;
					
				}
			
			if (billTotal > 0)
			{
				
				if(FragmentBillTotal.this.isVisible())
				{
					
					mWizardLocker.unlockNavigation(false, true, true);

				}
				
				else
				{
					
					mWizardLocker.unlockNavigation(true, true, true);
					
				}
				
			}
			
			else
			{
				
				mWizardLocker.lockNavigation(true, true, true);
				
			}

			mStandardVariables.billTotalSet(billTotal);
			
			mStandardVariables.calculateTotals();
		}
		
	};

}
